package com.ruoyi.student.controller;

import com.ruoyi.college.domain.College;
import com.ruoyi.college.domain.FirstLevelSubject;
import com.ruoyi.college.domain.SecondLevelSubject;
import com.ruoyi.college.service.ICollegeService;
import com.ruoyi.college.service.IFirstLevelSubjectService;
import com.ruoyi.college.service.ISecondLevelSubjectService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.student.domain.StudentInfo;
import com.ruoyi.student.domain.vo.StudentTrainingDetailsVo;
import com.ruoyi.student.service.IStudentInfoService;
import com.ruoyi.trainingManagement.service.ItrainingProgramCoursesService;
import com.ruoyi.trainingManagement.service.ItrainingProgramService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/student/studentTrainingDetails")
public class StudentTrainingDetailsController {

    @Autowired
    private IStudentInfoService studentInfoService;

    @Autowired

    private ItrainingProgramService trainingProgramService;

    @Autowired
    private ItrainingProgramCoursesService trainingProgramCoursesService;

    @Autowired
    private IFirstLevelSubjectService firstLevelSubjectService;

    @Autowired
    private ISecondLevelSubjectService secondLevelSubjectService;

    @Autowired
    private ICollegeService collegeService;

//    private IStudentInfoService studentInfoService;
    /**
     //     * 查询学生的培养方案信息，对应课程信息和个人信息
     //     */
    //@PreAuthorize("@ss.hasPermi('student:studentTrainingDetails:studentTrainingDetailsInfo')")
    @GetMapping("/studentTrainingDetailsInfo")
    public AjaxResult getStudentTrainingDetailsInfo()
    {
        LoginUser loginUser = SecurityUtils.getLoginUser(); // 获取当前登录用户对象
        String userName = loginUser.getUsername();
        System.out.println("打印获取到的用户信息"+userName);
        //获取当前登录学生的基本信息
        StudentInfo studentInfo = studentInfoService.selectStudentInfoByStudentId(userName);

        if (studentInfo == null) {
            return AjaxResult.error("未找到学生信息");
        }
        //获取学生志愿代码
        String StudentZy = studentInfo.getStudentZy();

        if (StudentZy!=null && StudentZy.endsWith("00")){
            FirstLevelSubject firstLevelSubject =firstLevelSubjectService.selectFirstLevelSubjectByFirstSubjectCode(StudentZy.substring(0,4));

            studentInfo.setStudentZy(firstLevelSubject.getFirstSubjectName()+"("+StudentZy.substring(0,4)+")");

        }else if (StudentZy!=null && !StudentZy.endsWith("00")){
            SecondLevelSubject secondLevelSubject = secondLevelSubjectService.selectSecondLevelSubjectBySecondSubjectCode(StudentZy);
            studentInfo.setStudentZy(secondLevelSubject.getSecondSubjectName()+"("+StudentZy+")");
        }

        College college = collegeService.selectCollegeByCollegeCode(studentInfo.getStudentYx());
        studentInfo.setStudentYx(college.getCollegeName());

        StudentTrainingDetailsVo studentTrainingDetailsVo = new StudentTrainingDetailsVo();
        studentTrainingDetailsVo.setStudentInfo(studentInfo);

        //获取学生对应的培养方案信息
        studentTrainingDetailsVo.setTrainingProgram(trainingProgramService.selecttrainingProgramById(studentInfo.getTrainingProgramId()));

        //获取培养方案对应的课程信息
        studentTrainingDetailsVo.setTrainingProgramCoursesList(trainingProgramCoursesService.selectStudentTrainingProgramCoursesVoByTrainingProgramId(studentInfo.getTrainingProgramId()));

        return AjaxResult.success(studentTrainingDetailsVo);

    }
}
